package com.neu.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.neu.question.beans.HttpResponseEntity;
import com.neu.question.common.Constans;
import com.neu.question.common.utils.SMSUtils;
import com.neu.question.common.utils.ValidateCodeUtils;
import com.neu.question.domain.UserInfo;
import com.neu.question.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * Created by wln on 2018\8\9 0009.
 */
@RestController
@RequestMapping("/admin")
public class UserController {

  private final Logger logger = LoggerFactory.getLogger(UserController.class);

  @Autowired
  private UserInfoService userService;

  @GetMapping("/tt")
  public String te() {
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username", "admin");
    UserInfo us = (UserInfo) userService.getOne(queryWrapper);
//        System.out.println(us.getId());
    return us.getPhoneNum();
  }

  /**
   * 核验密保问题
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/userMIBAOCheck", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity userMIBAOCheck(@RequestBody Map<String, String> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      if (!userService.userMIBAOCheck(map)) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.PASSWORDQESTION_CHECK_FAIL_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.PASSWORDQESTION_CHECK_SUCCESS_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("userLogin 密保验证>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 编辑密保问题
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/designPasswordQuestion", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity designPasswordQuestion(@RequestBody Map<String, String> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      if (!userService.insertQuestion(map)) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.PASSWORDQESTION_MESSAGE );
      }
    } catch (Exception e) {
      logger.info("userLogin 密保问题>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


  /**
   * 验证码请求
   *
   * @param
   * @return 验证码内容，
   */
  @RequestMapping(value = "/sendMsg", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity sendMsg(@RequestBody UserInfo userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    List<UserInfo> hasUser = userService.getUserByPhone(userEntity);
    try {
      if (CollectionUtils.isEmpty(hasUser)) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.NO_PHONENUM_MESSAGE);
      } else if (!CollectionUtils.isEmpty(hasUser) && hasUser.get(0).getStatus().equals("0")) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.LOGIN_USESTATUS_FROZEN_MESSAGE);
      } else {
        String code = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
        SMSUtils.sendMessage(userEntity.getPhoneNum(), code
          , SMSUtils.TEMPLATE_RESET_ID);
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(code);
        httpResponseEntity.setMessage(Constans.SEND_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("UserController 验证码发送>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


  /**
   * 验证码请求
   *
   * @param
   * @return 验证码内容，
   */
  @RequestMapping(value = "/getuserByPhone", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity getuserByPhone(@RequestBody UserInfo userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    List<UserInfo> hasUser = userService.getUserByPhone(userEntity);
    try {
      if (CollectionUtils.isEmpty(hasUser)) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.NO_PHONENUM_MESSAGE);
      } else if (!CollectionUtils.isEmpty(hasUser) && hasUser.get(0).getStatus().equals("0")) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.LOGIN_USESTATUS_FROZEN_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(hasUser.get(0));
        httpResponseEntity.setMessage(Constans.SEND_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("UserController 通过手机号获取用户信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 用户登录
   *
   * @param userEntity
   * @return
   */
  @RequestMapping(value = "/userLogins", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity userLogin(@RequestBody UserInfo userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      List<UserInfo> hasUser = userService.selectUserInfo(userEntity);
      if (CollectionUtils.isEmpty(hasUser)) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.LOGIN_USERNAME_PASSWORD_MESSAGE);
      } else if (!CollectionUtils.isEmpty(hasUser) && hasUser.get(0).getStatus().equals("0")) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.LOGIN_USESTATUS_FROZEN_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(hasUser);
        httpResponseEntity.setMessage(Constans.LOGIN_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("userLogin 用户登录>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 查询用户列表（模糊搜索）
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryUserList", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity queryUserList(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      PageInfo pageInfo = userService.queryUserList(map);
      if (pageInfo.getList().size() == 0) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setData(null);
        httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(pageInfo);
        httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
      }

    } catch (Exception e) {
//logger.info("userLogin 用户登录>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 创建用户的基本信息
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/addUserInfo", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity addUserInfo(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.addUserInfo(map);
      if (result == 3) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else if (result == 4) {
        httpResponseEntity.setMessage(Constans.USER_PHONERNAME_MESSAGE);
      } else if (result == 2) {
        httpResponseEntity.setCode(Constans.USER_TENANTSIDLOSE_CODE);
        httpResponseEntity.setMessage(Constans.USER_TENANTSIDLOSE_MESSAGE);
      } else if (result == 5) {
        httpResponseEntity.setCode(Constans.USER_TENANTSIDLOSE_CODE);
        httpResponseEntity.setMessage(Constans.USER_TENANTSID_MESSAGE);
      } else if (result == 6) {
        httpResponseEntity.setCode(Constans.USER_RESPONDENT_CODE);
        httpResponseEntity.setMessage(Constans.USER_RESPONDENT_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 创建用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 编辑用户的基本信息
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/modifyUserInfo", method = RequestMethod.POST, headers = "Accept=application/json")
  public HttpResponseEntity modifyUserInfo(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.modifyUserInfo(map);
      if (result == 3) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else if (result == 4) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.User_EXIST_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 编辑用户的基本信息>>>>>>>>>>>" + e);
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


  /**
   * 根据用户id查询用户基本信息
   *
   * @param userEntity
   * @return
   */
  @RequestMapping(value = "/selectUserInfoById", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity selectUserInfoById(@RequestBody UserInfo userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      Map<String, Object> result = userService.selectUserInfoById(userEntity);
      if (result.get("id").equals("")) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.LOGIN_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setData(result);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 通过ID查询用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 修改用户密码
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/modifyUserpassword", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity modifyUserpassword(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.modifyUserpassword(map);
      if (result != 1) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("deleteUserInfo 更新用户的密码>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


  /**
   * 修改用户状态
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/modifyUserStatus", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity modifyUserStatus(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.modifyUserStatus(map);
      if (result != 1) {
        //这种情况不存在 但为了应对将来出现管理员权限种类增加的情况需要加上这种判断
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage(Constans.USER_USERNAME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("deleteUserInfo 更新用户的权限>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 删除用户信息
   *
   * @param userEntity
   * @return
   */
  @RequestMapping(value = "/deleteUserInfoById", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity deteleUserInfoById(@RequestBody UserInfo userEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
//            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("id", userEntity.getId());
//            userService.remove(queryWrapper);
      int result = userService.deteleUserInfoById(userEntity);
      if (result == 3) {
        httpResponseEntity.setCode(Constans.USER_USERNAME_CODE);
        httpResponseEntity.setMessage("删除失败");
      } else if (result == 4) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.User_EXIST2_MESSAGE);
      } else if (result == 2) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage("删除租户失败");

      } else if (result == 5) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage("删除用户失败");
      } else if (result == 6) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage("删除答者失败");
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("deleteUserInfo 删除用户的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 用户没有权限
   *
   * @return
   */
  @RequestMapping(value = "/error")
  public HttpResponseEntity logout() {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    return httpResponseEntity;
  }

  /**
   * 忘记密码
   *
   * @return
   */
  @RequestMapping(value = "/forgetPassword", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity selectUserByPhone(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.selectUserByPhone(map);
      if (result == 0) {
        //不存在
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.LOGIN_PHONE_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        //发送验证码
        String bool = userService.sendCode(map);
        if (bool.equals("0")) {
          httpResponseEntity.setMessage(Constans.SEND_MESSAGE2);
        } else {
          httpResponseEntity.setData(bool);
          httpResponseEntity.setMessage(Constans.SEND_MESSAGE);
        }
      }
    } catch (Exception e) {
      logger.info("selectUserByPhone 查询是否存在此手机号的用户>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 验证验证码
   *
   * @return
   */
  //checkCode
  @RequestMapping(value = "/checkCode", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity checkCode(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    try {
      int result = userService.checkCode(map);
      if (result == 0) {
        //不存在
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.CHECK_MESSAGE2);
      } else {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.CHECK_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("selectUserByPhone 查询是否存在此手机号的用户>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }


}
